﻿Imports WiremetModel.Entities.SupplyOrders
Imports System.ComponentModel
Imports InputValidation
Imports InputValidation.GeneralRules

Public Class AddressInputDialog

    Private _unitOfWork As UnitOfWork

    Private _address As Address
    Public Property Address() As Address
        Get
            Return _address
        End Get
        Private Set(ByVal value As Address)
            _address = value
        End Set
    End Property

    Private _description As String
    Public Property Description() As String
        Get
            Return _description
        End Get
        Private Set(ByVal value As String)
            _description = value
        End Set
    End Property

    Private _inputValidator As InputValidator

    Public Sub New(Optional ByVal isFirstAddress As Boolean = False)

        InitializeComponent()
        _inputValidator = New InputValidator()
        _address = New Address()
        _unitOfWork = New UnitOfWork()
        ClearErrorLabels()

        tb_Description.Text = If(isFirstAddress, "Główny", String.Empty)

    End Sub

    Public Sub New(ByVal supplierAddress As SupplierAddress)

        Me.New()

        tb_AddressLine1.Text = supplierAddress.Address.AddressLine1
        tb_AddressLine2.Text = supplierAddress.Address.AddressLine2
        tb_AddressLine3.Text = supplierAddress.Address.AddressLine3
        tb_City.Text = supplierAddress.Address.City
        tb_Country.Text = supplierAddress.Address.Country
        tb_Description.Text = supplierAddress.Description
        tb_PostalCode.Text = supplierAddress.Address.PostalCode
        _address = supplierAddress.Address

    End Sub

    Private Sub AddressInputDialog_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles Me.FormClosed
        _unitOfWork.Dispose()
    End Sub

    Private Sub ClearErrorLabels()

        lbl_DescriptionError.Text = String.Empty
        lbl_CountryError.Text = String.Empty
        lbl_PostalCodeError.Text = String.Empty
        lbl_CityError.Text = String.Empty
        lbl_AddressLine1Error.Text = String.Empty

    End Sub


#Region "Input validation"


    Private Sub tb_Description_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_Description.Validating
        Dim notNullableInputSpecification As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_Description, e, lbl_DescriptionError, notNullableInputSpecification)
    End Sub

    Private Sub tb_Country_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_Country.Validating
        Dim notNullableInputSpecification As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_Country, e, lbl_CountryError, notNullableInputSpecification)
    End Sub

    Private Sub tb_PostalCode_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_PostalCode.Validating
        Dim notNullableInputSpecification As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_PostalCode, e, lbl_PostalCodeError, notNullableInputSpecification)
    End Sub

    Private Sub tb_City_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_City.Validating
        Dim notNullableInputSpecification As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_City, e, lbl_CityError, notNullableInputSpecification)
    End Sub

    Private Sub tb_AddressLine1_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_AddressLine1.Validating
        Dim notNullableInputSpecification As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_AddressLine1, e, lbl_AddressLine1Error, notNullableInputSpecification)
    End Sub

    Private Sub DescriptionInputValidated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_Description.Validated
        lbl_DescriptionError.Text = String.Empty
    End Sub

    Private Sub tb_Country_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_Country.Validated
        lbl_CountryError.Text = String.Empty
    End Sub

    Private Sub tb_PostalCode_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_PostalCode.Validated
        lbl_PostalCodeError.Text = String.Empty
    End Sub

    Private Sub tb_City_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_City.Validated
        lbl_CityError.Text = String.Empty
    End Sub

    Private Sub tb_AddressLine1_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_AddressLine1.Validated
        lbl_AddressLine1Error.Text = String.Empty
    End Sub


#End Region


    Private Sub btn_Accept_Click(sender As System.Object, e As System.EventArgs) Handles btn_Accept.Click
        Try
            If Not ValidateChildren() Then
                Throw New EntityDataInputException("Podane dane są nieprawidłowe." & vbNewLine &
                                                   "Proszę, popraw je i spróbuj ponownie.")
            End If

            Dim confirmationDialogResult As DialogResult = MessageBox.Show("Czy jesteś pewień?", "Uwaga!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If confirmationDialogResult = Windows.Forms.DialogResult.Yes Then

                With Address
                    .AddressLine1 = tb_AddressLine1.Text.Trim()
                    .AddressLine2 = If(String.IsNullOrEmpty(tb_AddressLine2.Text.Trim()), Nothing, tb_AddressLine2.Text.Trim())
                    .AddressLine3 = If(String.IsNullOrEmpty(tb_AddressLine3.Text.Trim()), Nothing, tb_AddressLine3.Text.Trim())
                    .City = tb_City.Text.Trim()
                    .Country = tb_Country.Text.Trim()
                    .PostalCode = tb_PostalCode.Text.Trim()
                End With


                'Dim addressSpecification As New SupplyOrders.AddressSpecification(Address)
                'Dim addressRepository As New Repository(Of Address)(_unitOfWork.Session)


                Dim geocoderLocationService As New GeocoderLocationService()
                Dim geoLocation As GMap.NET.PointLatLng = geocoderLocationService.Locate(Address.ToString())
                Address.Latitude = geoLocation.Lat
                Address.Longitude = geoLocation.Lng

                Description = tb_Description.Text.Trim()
                DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As EntityDataInputException
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


    Private Sub btn_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles btn_Cancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

End Class